package com.ly.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.reggie.common.CustomException;
import com.ly.reggie.mapper.CategoryMapper;
import com.ly.reggie.pojo.Category;
import com.ly.reggie.pojo.Dish;
import com.ly.reggie.pojo.Setmeal;
import com.ly.reggie.service.CategoryService;
import com.ly.reggie.service.DishService;
import com.ly.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;

    @Autowired
    SetMealService setMealService;


    /**
     * 根据id删除分类，删除之前需要判断
     *
     * @param id
     */
    @Override
    public void Remove(Long id) {
        //查询菜品关联情况
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //创建查询条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count_d = dishService.count(dishLambdaQueryWrapper);

        //查询当前分类是否关联了菜品，如果关联，抛出业务异常
        if (count_d > 0) {
            throw new CustomException("当前分类关联了菜品，不能删除");

        }

        //查询套餐关联情况
        LambdaQueryWrapper<Setmeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //创建查询条件
        setMealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count_s = setMealService.count(setMealLambdaQueryWrapper);


        //查询当前分类是否关联了套餐，如果关联，抛出业务异常
        if (count_s > 0) {
            throw new CustomException("当前分类关联了套餐，不能删除");
        }

        //都无关联，删除
        super.removeById(id);
    }
}
